package cn.alm.serviceorder.service;

import cn.alm.serviceorder.entity.Order;
import cn.alm.serviceorder.entity.Product;
import cn.alm.serviceorder.feign.ProductFeignClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private ProductFeignClient productFeignClient;

//    @Override
//    public Order createOrder(Long productId, Long userId) {
//        Order order = new Order();
//        order.setId(1L);
//        order.setUserId(userId);
//        // 这里商品列表应该service-product查询
//        Product productFromServiceProduct = getProductFromServiceProductAnnotation(productId);
//        ArrayList<Product> products = new ArrayList<>();
//        products.add(productFromServiceProduct);
//        order.setProductList(products);
//        BigDecimal price = productFromServiceProduct.getPrice();
//        order.setTotalAmount(price);
//        order.setUserAddress("北京");
//        order.setUsername("张三");
//        return order;
//    }

    @Override
    public Order createOrder(Long productId, Long userId) {
        Order order = new Order();
        order.setId(1L);
        order.setUserId(userId);
        // 这里商品列表应该service-product查询
//        Product productFromServiceProduct = getProductFromServiceProductAnnotation(productId);
        Product productById = productFeignClient.getProductById(productId);
        ArrayList<Product> products = new ArrayList<>();
        products.add(productById);
        order.setProductList(products);
        BigDecimal price = productById.getPrice();
        order.setTotalAmount(price);
        order.setUserAddress("北京");
        order.setUsername("张三");
        return order;
    }

    @Override
    @SentinelResource(value = "service hello2", blockHandler = "hello2Fallback")
    //    @SentinelResource(value = "service hello2")
    public String hello2() {
        return "This is hello2";
    }

    public String hello2Fallback(BlockException be) {
        System.out.println(be.getMessage());
        return "This is hello2 fallback";
    }

    private Product getProductFromServiceProduct(Long productId) {
        // 这里应该调用service-product查询商品信息
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url = choose.getUri() + "/product/get/" + productId;
        // 调用url
        return restTemplate.getForObject(url, Product.class);
    }

    private Product getProductFromServiceProductAnnotation(Long productId) {
        String url = "http://service-product/product/get/" + productId;
        // service-product：会被动态替换
        return restTemplate.getForObject(url, Product.class);
    }

}
